import { USER_KEY } from '@/constants' // 引入userinfo key 常量
import type { LoginParams } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'


export const useUserStore = defineStore(USER_KEY, () => {
  // token -----------------------------------------
  const token = ref() // token
  const setToken = (newToken: string) => { // 设置token
    token.value = newToken
  }
  const removeToken = () => { // 删除token
    token.value = ''
  }
  // 用户名密码 -----------------------------------------
  const loginData = ref<LoginParams | undefined>() // 登录信息
  const setLoginData = (newLoginData: LoginParams) => { // 设置登录信息
    loginData.value = newLoginData
  }
  const removeLoginData = () => { // 删除登录信息
    loginData.value = undefined
  }

  return {
    token,
    setToken,
    removeToken,
    loginData,
    setLoginData,
    removeLoginData
  }
}, { persist: true }) // 整个模块持久化
